#include "keyframedialog.h"
#include "ui_keyframedialog.h"

#include "actioncommands.h"
#include "history.h"
#include "global.h"
#include <QPushButton>
#include <QMessageBox>
#include <QStringBuilder>

KeyFrameDialog::KeyFrameDialog(QWidget *parent) :
    EditDialog(parent),
    ui(new Ui::KeyFrameDialog)
{
    ui->setupUi(this);

    //Disable the joint sliders from placing a command in the history
    //when a slider is changed.
    ui->jointSliders->enableUndo(false);

    ui->buttonBox->addButton(_deleteButton, QDialogButtonBox::DestructiveRole);

    setContextTip("Key Frame");
}

KeyFrameDialog::~KeyFrameDialog()
{
    delete ui;
}


void KeyFrameDialog::loadAction(RobotAction *action){
    EditDialog::loadAction(action);

    ui->jointSliders->loadModel(action->getRobot());
}


void KeyFrameDialog::prepDialog(int timestamp){
    EditDialog::prepDialog(timestamp);

    int i;
    bool exists = false;
    QList<int> jointIDs;

    //Set the dialog window title to contain the timestamp of the key frame we
    //are editing
    QString title = "Edit Key Frame - " % QString::number(MSEC_TO_SECF(_timestamp),'f', 2)
            % "s";
    this->setWindowTitle(title);

    //Find out if the key frame already exists.  If it does, then we need to
    //enable the delete button.
    if(_action != NULL){
        jointIDs = _action->jointIDs();
        for(i = 0; i < jointIDs.length(); i++){
            if(_action->keyValueAt(_timestamp, jointIDs[i]) != INVALID_KEYFRAME){
                exists = true;
                break;
            }
        }
    }

    if(exists){
        _deleteButton->setVisible(true);
    }
    else{
        _deleteButton->setVisible(false);
    }
}

void KeyFrameDialog::saveItem(){
    int i;
    //We need to grab the values from the joint sliders here, because
    //if a new action needs to be created, it sets the robot back
    //to rest, and the slider values will be invalidated.
    QList< QPair<int,float> > values = ui->jointSliders->values();
    ActionCommandGroup* actionGroupCommand;
    SetKeyFrameCommand* keyFrameCommand;

    //The save button was clicked.  Save the keyframe
    if(_action == NULL){
        //We don't have an action loaded, so this will bring up the
        //new action dialog and create a new action. The dialog is
        //modal, so control should return to this function after the
        //new action is loaded.
        emit needAction();
    }

    //Bundle the slider values into a group command
    actionGroupCommand = new ActionCommandGroup(_action);
    for(i = 0; i < values.length(); i++){
        keyFrameCommand = new SetKeyFrameCommand(_action, _timestamp, values[i].first,
                                                 values[i].second);
        actionGroupCommand->append(keyFrameCommand);
    }

    actionGroupCommand->setHistoryTip(tr("Set Key Frame"));
    actionGroupCommand->execute();
    History::instance()->push(actionGroupCommand);
}


void KeyFrameDialog::deleteItem(){
    //Create a delete key frame command
    DeleteKeyFrameCommand* deleteCommand = new DeleteKeyFrameCommand(_action, _timestamp);
    deleteCommand->setHistoryTip(tr("Delete Key Frame"));
    deleteCommand->execute();
    History::instance()->push(deleteCommand);
}

